﻿using AutoMapper;
using MediatR;
using Microsoft.Extensions.Logging;
using MS.Data.API.Write.Applicant.Command;
using MS.Data.Domain;
using MS.Data.ErrorCode;
using MS.Data.Infratructrue.Istanbul;

namespace MS.Data.API.Write.Applicant.CommandHandel
{

    public class XiuCommandHandel : IRequestHandler<XiuCommand, APIResult<int>>
    {
        IBaseRepository<Permission> perRep;
        private readonly IMapper mapper;

        public XiuCommandHandel(IBaseRepository<Permission> perRep,IMapper mapper)
        {
            this.perRep = perRep;
            this.mapper = mapper;
        }

        public async Task<APIResult<int>> Handle(XiuCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.code=APIEnum.更新成功;
            result.mes = APIEnum.更新成功.ToString();

            var updmodel = mapper.Map<Permission>(request);
            updmodel.PermissionId = request.PermissionId;
            var r = perRep.Updates(updmodel);


            updmodel.PermissionId = request.PermissionId;
            updmodel.PermissionName = request.PermissionName;
            updmodel.PermissionNo = request.PermissionNo;
            updmodel.PermissionType = request.PermissionType;


            if(r!>0)
            {
                result.code = APIEnum.更新成功;
                result.mes = APIEnum.更新成功.ToString();
                result.data = r;
            }
            
            
            return await Task.FromResult(result);

        }
    }
}
